package com.joeyzh.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by xuHuanLin on 2019/5/6.
 *
 * @author xuHuanLin
 * @Description 版本追溯使用
 */

public class VersionsRetrospect {


    /**
     * 获取版本名称
     *
     * @param context
     * @return
     */
    public static String packageName(Context context) {
        //获取包管理者
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            //获取包对应的信息
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            //获取版本名称
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    /**
     * 获取版本名称
     *
     * @param context
     * @return
     */
    public static String packageNameValue(Context context) {
        //获取包管理者
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            //获取包对应的信息
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            //获取版本名称
            name = "V" + info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    /**
     * 获取版本号
     *
     * @param context
     * @return
     */
    public static int packageCode(Context context) {
        //获取包管理者
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            //获取包对应的信息
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            //获取版本号
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }
}
